package com.danger.Adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.danger.Activity.R;
import com.danger.Model.Node;
import com.danger.Util.TreeHelper;

import java.util.List;

/**
 * Created by 俊 on 2016/6/10.
 */
public class SimpleTreeListViewAdapter<T> extends TreeListViewAdapter<T> {

    public SimpleTreeListViewAdapter(ListView listView, Context context, List datas, int defaultExpandLevel)
            throws IllegalAccessException, IllegalArgumentException {
        super(listView, context, datas, defaultExpandLevel);
    }

    @Override
    public View getConvertView(Node node, int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (holder == null) {
            //传两个参数的话，根布局的属性是不管用的
            convertView = super.mInflater.inflate(R.layout.listview_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (node.getIcon() == -1) {
            //VISIBLE和GONE的区别：
            //VISIBLE是隐藏，而GONE是彻底的移除
            holder.item_icon_imageView.setVisibility(View.INVISIBLE);
        } else {
            holder.item_icon_imageView.setVisibility(View.VISIBLE);
            holder.item_icon_imageView.setImageResource(node.getIcon());
        }
        holder.item_text_textView.setText(node.getName());
        return convertView;
    }

    /**
     * 动态插入节点
     *
     * @param position
     * @param string
     */
    public void addExtraNode(int position, String string) {
        Node node = mVisibleNodes.get(position);
        int indexOf = mAllNodes.indexOf(node);
        Node extraNode = new Node(-1, node.getId(), string);
        extraNode.setParent(node);
        node.getChildren().add(extraNode);
        mAllNodes.add(indexOf + 1, extraNode);
        mVisibleNodes = TreeHelper.filterVisibleNodes(mAllNodes);
        notifyDataSetChanged();
    }

    private class ViewHolder {
        ImageView item_icon_imageView;
        TextView item_text_textView;

        public ViewHolder(View view) {
            item_icon_imageView = (ImageView) view.findViewById(R.id.item_icon_imageView);
            item_text_textView = (TextView) view.findViewById(R.id.item_text_textView);
        }
    }
}
